#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int to_binary(int num)
{
	int i=1, binary=0;

	while(num>0)
	{
		binary+=(num%2)*i;
		num/=2;
		i*=10;
	}
	return binary;
}
void print_roman(int num)
{
	while(num>=1000)
	{
		printf("M");
		num-=1000;
	}
	if(num>=900)
	{
		printf("CM");
		num-=900;
	}
	if(num>=500)
	{
		printf("D");
		num-=500;
	}
	if(num>=400)
	{
		printf("CD");
		num-=400;
	}
	while(num>=100)
	{
		printf("C");
		num-=100;
	}
	if(num>=90)
	{
		printf("XC");
		num-=90;
	}
	if(num>=50)
	{
		printf("L");
		num-=50;
	}
	if(num>=40)
	{
		printf("XL");
		num-=40;
	}
	while(num>=10)
	{
		printf("X");
		num-=10;
	}
	if(num>=9)
	{
		printf("IX");
		num-=9;
	}
	if(num>=5)
	{
		printf("V");
		num-=5;
	}
	if(num>=4)
	{
		printf("IV");
		num-=4;
	}
	while(num>=1)
	{
		printf("I");
		num-=1;
	}
}
void printFormattedIntegers(char *format, char* numbers)
{
	/*
	int numbers_i[10];
	int count = 0;
	numbers = strtok(numbers, " ");
	while(numbers != NULL)
	{
		numbers_i[count] = atoi(numbers);
		count++;
		numbers = strtok(NULL, " ");
	}
	*/
	int i, number, flag;
	flag = 0;
	i=0;
	number = atoi(strtok(numbers, " "));
	while(format[i]!='\0')
	{
		if(format[i]!='%')
			putchar(format[i]);
		else
		{
			switch(format[i+1])
			{
				case 'd':
					if(flag) number = atoi(strtok(NULL, " "));
					i++;
					printf("%d", number);
					flag = 1;
					break;
				case 'x':
					if(flag) number = atoi(strtok(NULL, " "));
					i++;
					flag = 1;
					printf("%x", number);
					break;
				case 'b':
					if(flag) number = atoi(strtok(NULL, " "));
					i++;
					flag = 1;
					printf("%d", to_binary(number));
					break;
				case 'o':
					if(flag) number = atoi(strtok(NULL, " "));
					i++;
					flag = 1;
					printf("%o", number);
					break;
				case 'r':
					if(flag) number = atoi(strtok(NULL, " "));
					i++;
					flag = 1;
					print_roman(number);
					break;
				default:
					putchar('%');
			}
		}
		i++;
	}
	

}


int main()
{

	char format[100];

	char numbers[100];

	gets(format);

	gets(numbers);

	printFormattedIntegers(format, numbers);
	
	return 0;
}

